export function formatMenuData (menus) {
  let result = []

  // handle submenu
  menus.forEach((menu, index) => {
    if (!menu.parent) {
      menu.routeName = menu.name
      menu.titles = [menu.title]
      menu.children = []
      result.push(menu)
      menus.splice(index, 1)
    }
  })

  // handle menu item
  let hasNoParentMenu = false
  while (!hasNoParentMenu && menus.length > 0) {
    hasNoParentMenu = true
    menus.forEach((menu, index) => {
      const parentIndex = result.findIndex(element => element.id === menu.parent)
      if (parentIndex >= 0) {
        const parent = result[parentIndex]
        menu.routeName = menu.name
        menu.titles = parent.titles.concat([menu.title])
        parent.children.push(menu)
        menus.splice(index, 1)
        hasNoParentMenu = false
      }
    })
  }
  return result
}
